package com.byq.mm.smartlib.tools;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.byq.mm.smartlib.R;

import com.byq.mm.abstractx.device.Device;
import com.byq.mm.smartlib.cluster.activity.client.DeviceActivityManager;
import com.byq.mm.common.engine.Contact;
import com.byq.mm.smartlib.application.SmartApplication;


public class RosterAdapter extends BaseExpandableListAdapter{
	private LayoutInflater mInflater;
	Context context;
	public RosterAdapter(Context ctx, SmartApplication application) {
		super();
		this.application = application;
		this.context = ctx;
		mInflater = LayoutInflater.from(ctx);
		setData();
	}
	SmartApplication application;

	Contact[] entrys;

	void setData(){

	
			entrys = application.getAndroidSmartEngine().getXmppEngine().getContactManager().getContacts().toArray(new Contact[0]);
			
		
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return entrys[childPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}
	int getChatResource(Contact p){
		if(p.isOnline()) {
			if(p.getMode() == Contact.Mode.away) {
				return R.drawable.presence_away;
			} else if (p.getMode() == Contact.Mode.xa) {
				return R.drawable.kopete_all_away;
			} else if (p.getMode() == Contact.Mode.dnd) {
				return R.drawable.presence_unknown;
			} else if (p.getMode() == Contact.Mode.chat) {
				return R.drawable.metacontact_online;
			} else {
				return R.drawable.presence_online;
			}
		} else {
			return R.drawable.presence_offline;
			
		}
	}
	
	@Override
	public View getChildView(int gp, int cp,
			boolean isLastChild, View cv, ViewGroup parent) {
		
		
		
		
		
		if(cv == null){
			cv = mInflater.inflate(R.layout.list_item_icon_text_arrow, null);
		}
			
			final Contact c = entrys[cp];
			final Device d = application.getAndroidSmartEngine().getCoreEngine().getClientDeviceManager().getRootDevice(c.getTransport());

			TextView name = (TextView)cv.findViewById(R.id.text);
			
			ImageView icon = (ImageView)cv.findViewById(R.id.icon);
			
			
			
			
			
			ImageView arrow = (ImageView)cv.findViewById(R.id.arrow);
			if(d == null || d.getSubdevices().isEmpty())
				arrow.setVisibility(View.GONE);
			else
				arrow.setVisibility(View.VISIBLE);
			String sn = c.getName();
			if(sn == null || sn.trim().equals("")){
				sn = c.getTransport();
				sn = sn.split("@")[0];
			}
			name.setText(sn);
			arrow.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
//					Intent intent = new Intent();
//					intent.setClass(context, ContactDetailActivity.class);
//					intent.putExtra("jid", c.getUser());
//					context.startActivity(intent);
					if(d == null)
						return;
					Intent i = new Intent();
					i.setPackage(context.getPackageName());
					i.setClass(context, DeviceActivityManager.getSubdeviceActivity(d.getType()));
					i.putExtra("globalId", d.getGlobalId());
					context.startActivity(i);
					return;
					
				}
			});
			
		//	ImageView control = (ImageView)cv.findViewById(R.id.device_controll);
			if(d != null){
		//		control.setVisibility(View.VISIBLE);
				String type = d.getType();
				if (type == null || type.equals(""))
					type = "device";
				Drawable da = null;
				if(c.isOnline())
					da = IconManager.getIconDrawable(context, type);
				else
					da = IconManager.getGrayIconDrawable(context, type);
				icon.setImageDrawable(da);
			}else
				icon.setImageResource(getChatResource(c));
//			ToggleButton permit = (ToggleButton)cv.findViewById(R.id.device_permit);
//			boolean km = application.getServerDeviceManager().isKnownTo(c.getUser());
//			boolean permitted = application.getServerDeviceManager().permitted(c.getUser());
//			if(!km)
//				permit.setVisibility(View.GONE);
//			else
//				permit.setVisibility(View.VISIBLE);
//			permit.setPressed(permitted);

		return cv;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return entrys.length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return "";
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,40);
		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setPadding(40, 0, 0, 0);
	
		textView.setTextSize(20);

			textView.setText(R.string.friends);
		return textView;

	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	@Override
	public void notifyDataSetChanged() {
		setData();
		super.notifyDataSetChanged();
	}
}